package cn.com.xyc.action;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import cn.com.xyc.common.ALiYunTool;
import cn.com.xyc.pojo.PicGroupInfo;
import cn.com.xyc.pojo.PicInfo;
import cn.com.xyc.service.ToolService;

public class ToolAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	private ToolService toolService;

	private File groupIcon;
	private String groupIconContentType;
	private String groupIconFileName;

	private File uploadPics;
	private String uploadPicsContentType;
	private String uploadPicsFileName;

	private PicGroupInfo picGroupInfo;
	private PicInfo picInfo;

	private String hiddenVal;
	private String tipInfo;
	private String check;
	private Map<String, Object> jsonMap;
	private List<PicGroupInfo> picGroupInfoList;
	private List<PicInfo> picInfoList;
	

	/**
	 * 图片上传跳转页
	 * 
	 * @return
	 */
	public String photoUpload() {
		try {
			picGroupInfoList = toolService.selectPicGroupInfos();
			if(check!=null){
				if(check.equals("1")){
					tipInfo="您的图片已添加成功！";
				}else{
					tipInfo="您的图片添加失败!";
				}
			}
		} catch (Exception e) {
		}
		return "photoUpload";
	}

	public String savePicInfos() {
		try {
			if (uploadPics != null) {
				String uploadPath = "picImg/"
						+ System.currentTimeMillis()
						+ uploadPicsFileName.substring(uploadPicsFileName
								.lastIndexOf("."));
				String url = ALiYunTool.uploadGroupIcon(uploadPics, "picdata",
						uploadPath);
				picInfo.setPicUploadAddress(url);
				int num=toolService.insertPicInfo(picInfo, getCurrentUser(), uploadPics);
				if(num>0){
					check="1";
				}else{
					check="0";
				}
			}
		} catch (Exception e) {
			check="0";
		}
		return "savePicSuccess";
	}

	/**
	 * 上传图片类别图标
	 * 
	 * @return
	 */
	public String uploadGroupIcon() {
		try {
			if (groupIcon != null) {
				String uploadPath = "groupIcon/"
						+ System.currentTimeMillis()
						+ groupIconFileName.substring(groupIconFileName
								.lastIndexOf("."));
				String url = ALiYunTool.uploadGroupIcon(groupIcon, "picdata",
						uploadPath);
				jsonMap = new HashMap<String, Object>();
				jsonMap.put("result", url);
				jsonMap.put("hiddenVal", hiddenVal);
				return "ajaxReturn";
			} else {
				return null;
			}
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 保存图片组信息
	 * 
	 * @return
	 */
	public String savePicGroupInfo() {
		int result;
		jsonMap = new HashMap<String, Object>();
		try {
			result = toolService.insertPicGroupInfo(picGroupInfo,
					getCurrentUser());
			if (result > 0) {
				jsonMap.put("result", "success");
			} else {
				jsonMap.put("result", "fail");
			}
		} catch (Exception e) {
			jsonMap.put("result", "fail");
		}
		return "ajaxReturn";
	}
	
	public String picBrowse(){
		try {
			picInfoList=toolService.selectPicInfos();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "picBrowse";
	}
	

	public PicGroupInfo getPicGroupInfo() {
		return picGroupInfo;
	}

	public void setPicGroupInfo(PicGroupInfo picGroupInfo) {
		this.picGroupInfo = picGroupInfo;
	}

	public File getGroupIcon() {
		return groupIcon;
	}

	public void setGroupIcon(File groupIcon) {
		this.groupIcon = groupIcon;
	}

	public String getGroupIconContentType() {
		return groupIconContentType;
	}

	public void setGroupIconContentType(String groupIconContentType) {
		this.groupIconContentType = groupIconContentType;
	}

	public String getGroupIconFileName() {
		return groupIconFileName;
	}

	public void setGroupIconFileName(String groupIconFileName) {
		this.groupIconFileName = groupIconFileName;
	}

	public Map<String, Object> getJsonMap() {
		return jsonMap;
	}

	public void setJsonMap(Map<String, Object> jsonMap) {
		this.jsonMap = jsonMap;
	}

	public String getHiddenVal() {
		return hiddenVal;
	}

	public void setHiddenVal(String hiddenVal) {
		this.hiddenVal = hiddenVal;
	}

	public List<PicGroupInfo> getPicGroupInfoList() {
		return picGroupInfoList;
	}

	public void setPicGroupInfoList(List<PicGroupInfo> picGroupInfoList) {
		this.picGroupInfoList = picGroupInfoList;
	}

	public File getUploadPics() {
		return uploadPics;
	}

	public void setUploadPics(File uploadPics) {
		this.uploadPics = uploadPics;
	}

	public String getUploadPicsContentType() {
		return uploadPicsContentType;
	}

	public void setUploadPicsContentType(String uploadPicsContentType) {
		this.uploadPicsContentType = uploadPicsContentType;
	}

	public String getUploadPicsFileName() {
		return uploadPicsFileName;
	}

	public void setUploadPicsFileName(String uploadPicsFileName) {
		this.uploadPicsFileName = uploadPicsFileName;
	}

	public PicInfo getPicInfo() {
		return picInfo;
	}

	public void setPicInfo(PicInfo picInfo) {
		this.picInfo = picInfo;
	}

	public String getTipInfo() {
		return tipInfo;
	}

	public void setTipInfo(String tipInfo) {
		this.tipInfo = tipInfo;
	}

	public String getCheck() {
		return check;
	}

	public void setCheck(String check) {
		this.check = check;
	}

	public List<PicInfo> getPicInfoList() {
		return picInfoList;
	}

	public void setPicInfoList(List<PicInfo> picInfoList) {
		this.picInfoList = picInfoList;
	}

}
